#!/usr/bin/python
import pygame, sys,os, gameview.utilities, gameview.fontmap
from pygame.locals import * 

class AetherModel:     
	"""AetherModel will be in charge of managing game state, networking, and managing game events"""
	def setup_world(self):
		"""right now the world is just a  multdimensional array of characters"""
		self.world = []
		for y in range(10):
			charRow = []
			for x in range(10):
				charRow.insert(x,'#')
			self.world.insert(y,charRow)
	def get_world(self):
		return self.world
	def set_controller(self, controller):
		self.controller = controller

class AetherView:      
	"""AetherView will be in charge of putting together screens, and taking updates to its viewstate from the model"""
	def setup_window(self):
		
		"""This is just basic pygame initialization calls"""
		pygame.init() 
		window = pygame.display.set_mode((800, 600)) 
		pygame.display.set_caption('Aether') 
		self.screen = pygame.display.get_surface() 

		"""This is where special input setup should go, right now we have keys repeat every 10ms"""
		pygame.key.set_repeat(10,10)

		"""Lets set the icon of the application"""
		aether_surface, aether_rect = gameview.utilities.load_image("aether.jpg")
		pygame.display.set_icon(aether_surface)
	
	def load_resources(self):
		"""Make the font map"""
		self.font_map = gameview.fontmap.FontMap("fontmap.gif",16,16)
		self.unitwidth,self.unitheight = self.font_map.get_unitsize()
		"""Make a basic green background we can re-use again and again"""
		self.background_surface = pygame.Surface(self.screen.get_size())
		self.background_surface.fill(gameview.utilities.parse_webcolor("006600"))
		
		"""For now we just have a big @ symbol as a player, we can pass along a color to get the color of the symbol"""
		self.letters_surface = self.font_map.get_lettersurface('@',(0,255,0))
	
	def createterrain(self, world):
		"""This function helps us create the surface that will represent the terrain detail"""
		terrainUnitHeight = len(world)
		terrainUnitWidth = len(world[0])
		self.surface_terrain = pygame.Surface((terrainUnitWidth*self.unitwidth,terrainUnitHeight*self.unitheight))
		for y in range(terrainUnitHeight):
			for x in range(terrainUnitWidth):
				surface_terrainDetail = self.font_map.get_lettersurface(world[y][x],(0,255,0))
				self.get_xy(x,y)
				self.surface_terrain.blit(surface_terrainDetail, self.get_xy(x,y))
		self.terrainLoaded = True
	
	def get_xy(self,unitx,unity):
		"""takes in unit x and y and returns an x y"""
		pos = (unitx*self.unitwidth, unity*self.unitheight)
		return pos

	def get_nearestxy(self,x,y):
		"""takes in an x y value and returns an x y that is at the nearest unit point"""
		pos = ((float(x)/float(self.unitwidth))*float(self.unitwidth),(float(y)/float(self.unitheight))*float(self.unitheight))
		return pos
	def set_controller(self, controller):
		self.controller = controller
	def run(self):
		self.x = 0
		self.y = 0
		while True:
			"""draw the base background"""
			self.screen.blit(self.background_surface, (0,0)) 
			
			"""draw the terrain detail"""
			if self.terrainLoaded:
				self.screen.blit(self.surface_terrain, (0,0))

			self.screen.blit(self.letters_surface, self.get_nearestxy(self.x,self.y))
			pygame.display.flip() 
   			self.input(pygame.event.get())

	def input(self, events): 
   		for event in events: 
      			if event.type == QUIT: 
        			sys.exit(0)    
      			if event.type == KEYDOWN and event.key == K_UP:
				self.y-=1
      			if event.type == KEYDOWN and event.key == K_DOWN:
				self.y+=1
      			if event.type == KEYDOWN and event.key == K_LEFT:
				self.x-=1
			if event.type == KEYDOWN and event.key == K_RIGHT:
				self.x+=1
	      		if event.type == KEYDOWN and event.key == K_ESCAPE:
				sys.exit(0)
      			else: 
         			"""print event """
	
class AetherController:
	def __init__(self, model, view):
        	self.model = model
		self.model.set_controller(self)
        	self.view = view
		self.view.set_controller(self)
	def run(self):
		self.view.setup_window()
		self.view.load_resources()
		self.model.setup_world()
		self.updatecurrentterrain()
		self.view.run()
	def updatecurrentterrain(self):
		self.view.createterrain(self.model.get_world())

if __name__ == '__main__':
	"""MVC helps us separate the concerns of the application View(2d stuff,input) from Model(networking,game logic)"""
	model = AetherModel()
	view = AetherView()
	controller = AetherController(model,view)
	controller.run()


